/*    */ package com.zimbra.cs.security.sasl;
/*    */ 
/*    */ import java.nio.ByteBuffer;
/*    */ import javax.security.sasl.SaslException;
/*    */ import org.apache.mina.core.buffer.IoBuffer;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class SaslInputBuffer
/*    */ {
/*    */   private final int mMaxSize;
/*    */   private final ByteBuffer mLenBuffer;
/*    */   private ByteBuffer mDataBuffer;
/*    */   
/*    */   public SaslInputBuffer(int maxSize)
/*    */   {
/* 32 */     this.mMaxSize = maxSize;
/* 33 */     this.mLenBuffer = ByteBuffer.allocate(4);
/*    */   }
/*    */   
/*    */   public void put(IoBuffer buf) throws SaslException {
/* 37 */     put(buf.buf());
/*    */   }
/*    */   
/*    */   public void put(ByteBuffer bb) throws SaslException {
/* 41 */     if (isComplete()) return;
/* 42 */     if ((this.mLenBuffer.hasRemaining()) && (!readLength(bb))) return;
/* 43 */     int len = Math.min(this.mDataBuffer.remaining(), bb.remaining());
/* 44 */     int pos = this.mDataBuffer.position();
/* 45 */     bb.get(this.mDataBuffer.array(), pos, len);
/* 46 */     this.mDataBuffer.position(pos + len);
/*    */   }
/*    */   
/*    */   public boolean isComplete() {
/* 50 */     return (this.mDataBuffer != null) && (!this.mDataBuffer.hasRemaining());
/*    */   }
/*    */   
/*    */   public int getLength() {
/* 54 */     return this.mDataBuffer != null ? this.mDataBuffer.limit() : -1;
/*    */   }
/*    */   
/*    */   public int getRemaining() {
/* 58 */     return this.mDataBuffer != null ? this.mDataBuffer.remaining() : -1;
/*    */   }
/*    */   
/*    */   public byte[] unwrap(SaslSecurityLayer securityLayer) throws SaslException {
/* 62 */     if (!isComplete()) {
/* 63 */       throw new IllegalStateException("input not complete");
/*    */     }
/* 65 */     return securityLayer.unwrap(this.mDataBuffer.array(), 0, this.mDataBuffer.position());
/*    */   }
/*    */   
/*    */   public void clear()
/*    */   {
/* 70 */     this.mLenBuffer.clear();
/* 71 */     if (this.mDataBuffer != null) this.mDataBuffer.clear();
/*    */   }
/*    */   
/*    */   private boolean readLength(ByteBuffer bb) throws SaslException
/*    */   {
/* 76 */     while (this.mLenBuffer.hasRemaining()) {
/* 77 */       if (!bb.hasRemaining()) return false;
/* 78 */       this.mLenBuffer.put(bb.get());
/*    */     }
/* 80 */     int len = this.mLenBuffer.getInt(0);
/* 81 */     if ((len < 0) || (len > this.mMaxSize)) {
/* 82 */       throw new SaslException("Invalid receive buffer size '" + len + "' (max size = " + this.mMaxSize + ")");
/*    */     }
/*    */     
/*    */ 
/* 86 */     if ((this.mDataBuffer == null) || (this.mDataBuffer.capacity() < len)) {
/* 87 */       this.mDataBuffer = ByteBuffer.allocate(len);
/*    */     }
/* 89 */     this.mDataBuffer.limit(len);
/* 90 */     return true;
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/security/sasl/SaslInputBuffer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */